// ==UserScript==
// @name         GitSpider
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://gitee.com/*
// @match        http://gitlab.tiandy.com/*
// @icon         
// @grant        GM_xmlhttpRequest
// @require      https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js
// ==/UserScript==

(function () {
    'use strict';
    const config = {
        log: true,
        uploadApi: 'http://182.92.148.42/api/uploadSubmitData',
        origin: window.location.origin,
        gitLabUserPath: '/liuyi09942',
        giteeUserPath: '/YveTraveler'
    };
    console.log('liuyi');
    if (window.location.pathname != config.gitLabUserPath && window.location.pathname != config.giteeUserPath) {
        console.log('pathName配置错误', window.location.pathname);
        return;
    }
    console.clear();
    if (!config.log) {
        console.log = () => {};
    }
    console.log('开始=========');
    if (config.origin == 'https://gitee.com') {
        console.log('开始gitee.com =========');
        //DOM 获取NodeList
        let nodeList = document.getElementsByClassName('right-side')[0].getElementsByClassName('box');
        console.log('原始dom right-side', nodeList.length, nodeList);
        //获取信息
        let infoArr = Array.from(nodeList);
        console.log('原始数组infoArr', infoArr);
        const infoData = [];
        infoArr.forEach(item => {
            let date, regex, numList;
            try {
                date = item.getAttribute('date');
                //正则匹配num
                regex = /0|[1-9][0-9]*/;
                numList = regex.exec(item.getAttribute('data-content'));
            } catch (error) {
                console.error('获取dom数据错误');
            }
            console.log(numList);
            if (!date || !numList || numList[0] == 0) {
                return;
            }
            infoData.push({ date, num: numList[0] });
        });
        console.log('数据', infoData);
        //axios发送数据
        axios.post(config.uploadApi, { data: infoData, type: 'gitee' }).then(res => {
            console.log(res.status == 200 ? '成功:' : '失败:', res.status, res.message);
        });
    } else if (config.origin == 'http://gitlab.tiandy.com') {
        axios
            .get(`/users${config.gitLabUserPath}/calendar.json`)
            .then(res => {
                return new Promise((resolve, reject) => {
                    const resDataObj = res.data;
                    if (Object.prototype.toString.call(resDataObj) != '[object Object]') {
                        reject('查询的参数错误');
                    }
                    //转换成数组的形式
                    const commitArr = [];
                    for (const date in resDataObj) {
                        if (Object.hasOwnProperty.call(resDataObj, date)) {
                            const num = resDataObj[date];
                            commitArr.push({ date: date, num });
                        }
                    }
                    resolve(commitArr);
                });
            })
            .then(res => {
                // console.info('liuyi ==>res', res);
                //axios发送数据
                axios.post(config.uploadApi, { data: res, type: 'gitlab' }).then(res => {
                    console.info('liuyi ==>res', res);
                    const isError = res.data.status != 0;
                    if (isError) {
                        console.log('失败:', res.data.status, res.data.message);
                    } else {
                        console.log('成功:', res.data.status, res.data.message);
                    }
                });
            });
    }
})();
